import { JXDef } from '../../conventions/JXCommon';
import { GCtrl } from '../../Core/GCtrl';
import { BUFFINFO, OFFICERITEM, VIPRECEIVE } from '../Common/Define';
import { ObjectWrap } from './../../Core/FrameEx/ES5Ex';
import GameMgr from './GameMgr';


export default class UserMgr extends ObjectWrap {
    private static _instance: UserMgr = null;
    public static ins(): UserMgr {
        if (this._instance == null) {
            this._instance = new UserMgr();
        }
        return this._instance;
    }

    constructor() {
        super();
    }

    /** 获取下一个目标关卡信息 */
    public getCurLevelRaw() {
        let levelId = GameMgr.lUserData.level + 1;
        let curRaw = GameMgr.storyData.tryGetRaw<SStoryDataRaw>(levelId);
        if (!curRaw) {
            curRaw = GameMgr.storyData.getRaw<SStoryDataRaw>(levelId - 1);
        }
        return curRaw;
    }

    /**判断vip奖励*/
    public checkVipReward(id: number, isFrist: boolean) {
        let vipRewards: vipReward[] = GameMgr.lUserData.vipReward;
        let vipReward = vipRewards[id - 1];
        if (!vipReward) return false;
        if (isFrist) {
            if (vipReward.recycle === VIPRECEIVE.No) {
                return false;
            }
            return vipReward.recycle === VIPRECEIVE.FIRSTNORECEIVE
        } else {
            if (vipReward.recycleUp === VIPRECEIVE.No) {
                return false;
            }
            return vipReward.recycleUp === VIPRECEIVE.DATNORECEIVE
        }
    }

    /**是否已领取 */
    public vipReward(id: number, isFrist: boolean) {
        let vipRewards: vipReward[] = GameMgr.lUserData.vipReward;
        let vipReward = vipRewards[id - 1];
        if (!vipReward) return false;
        if (isFrist) {
            if (vipReward.recycle === VIPRECEIVE.FIRSTRECEIVE) {
                return true;
            } else {
                false;
            }
        } else {
            if (vipReward.recycleUp === VIPRECEIVE.DAYRECEIVE) {
                return true;
            } else {
                return false;
            }
        }
    }

    public checkVipRedPoint(isFrist: boolean) {
        let data = GameMgr.vipData.data.values<SVipDataRaw>()
        let value = false;
        for (let i = 0; i < data.length; i++) {
            let vip = data[i];
            value = GameMgr.userMgr.checkVipReward(vip.id, isFrist)
            if (value) break;
        }
        return value;
    }

    public checkOfficeReward(id: number) {
        let office: OfficeBox = GameMgr.lUserData.getOfficeStatus(id);
        if (!office) {
            return false;
        }
        if (office.box === OFFICERITEM.NOT) {
            return false;
        }
        return office.box === OFFICERITEM.LOCK;
    }

    public checkOfficeRedPoint() {
        let data = GameMgr.officerData.data.values<SOfficerDataRaw>();
        let value = false;
        for (let i = 0; i < data.length; i++) {
            let office = data[i];
            value = GameMgr.userMgr.checkOfficeReward(office.id)
            if (value) break;
        }
        return value;
    }

    public checkSignRedPoint() {
        let value = !GameMgr.lUserData.todayIsSignIn();
        return value;
    }

    public checkTowerRewardTime() {
        let time = GameMgr.lUserData.lastTowerGet;
        let now = GCtrl.now;
        let towerInfo = GameMgr.lUserData.getRewardInfo();
        return (now - time >= 12 * JXDef.Time.HOUR && towerInfo[0] && towerInfo[1]);
    }

    public checkCanBuyFree() {
        return
        //每日免费获取装备次数
        var freeDay: number = GameMgr.systemConfig.value(JXDef.SYS_CONFIG_KEY.free);
        let freeBuff: number = GameMgr.lUserData.getUserBuff(BUFFINFO.storeDayFreeBuyNum);
        if (freeBuff > 0) {
            freeDay += freeBuff;
        }
        //当天已使用免费天数
        var userFree = GameMgr.lUserData.free;
        //是否可以有免费使用
        var isUseFree: boolean = false;
        //当天是否已有使用过免费次数
        if (GameMgr.lUserData.todayIsUseDayFreeDate()) {
            //使用次数是否在配置值范围
            if (userFree <= freeDay) {
                isUseFree = false;
            }
        } else {
            //当天未使用过免费次数 将存储数据设置为0
            GameMgr.lUserData.free = 0;
            //可以使用免费次数
            isUseFree = true;
        }
        return isUseFree;
    }

}